package com.chenjim.architecture.screen.shimme

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.foundation.layout.asPaddingValues
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.statusBars
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowBack
import androidx.compose.material.icons.filled.LocationOn
import androidx.compose.material.icons.filled.Place
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavHostController
import androidx.navigation.compose.rememberNavController
import com.chenjim.architecture.screen.Order
import com.chenjim.architecture.screen.shimme.ShimmerScreenVM.UiIntent
import com.chenjim.architecture.screen.shimme.ShimmerScreenVM.UiState
import com.valentinilk.shimmer.shimmer

@Composable
fun ShimmerScreen(
    navController: NavHostController,
    viewModel: ShimmerScreenVM = hiltViewModel()
) {
    val viewState = viewModel.uiState.collectAsState().value
    viewModel.processIntent(UiIntent.InitIntent)

    Surface(modifier = Modifier.fillMaxSize(), color = MaterialTheme.colorScheme.background) {
        Column {
            // 顶部AppBar
            Box(
                modifier = Modifier
                    .fillMaxWidth()
                    .padding(WindowInsets.statusBars.asPaddingValues()),
                contentAlignment = Alignment.Center
            ) {
                Row(
                    verticalAlignment = Alignment.CenterVertically,
                    modifier = Modifier.fillMaxWidth()
                ) {
                    IconButton(
                        onClick = { /* 返回逻辑 */ },
                    ) {
                        Icon(
                            imageVector = Icons.Default.ArrowBack,
                            contentDescription = "Back",
                            tint = MaterialTheme.colorScheme.onBackground
                        )
                    }
                    Text(
                        text = "历史订单",
                        style = MaterialTheme.typography.titleLarge,
                        color = MaterialTheme.colorScheme.onBackground
                    )
                }
            }

            // 订单列表
            LazyColumn(
                modifier = Modifier.padding(horizontal = 16.dp),
                verticalArrangement = Arrangement.spacedBy(16.dp)
            ) {
                if (viewState.isLoading) {
                    items(5) { index ->
                        OrderItemView(
                            order = Order(
                                type = "              ",
                                status = "          ",
                                time = "                                   ",
                                startAddress = "                                        ",
                                endAddress = "                                         "
                            ),
                            viewState = viewState
                        )
                    }
                } else {
                    items(viewState.data.size) { index ->
                        OrderItemView(
                            order = viewState.data[index],
                            viewState = viewState
                        )
                    }
                }
            }
        }
    }
}

@Composable
fun OrderItemView(order: Order, viewState: UiState) {
    Card(
        modifier = Modifier
            .fillMaxWidth()
            .padding(vertical = 10.dp)
            .then(if (viewState.isLoading) Modifier.shimmer() else Modifier),
        shape = RoundedCornerShape(16.dp),
        elevation = CardDefaults.cardElevation(4.dp),
        colors = CardDefaults.cardColors(containerColor = Color.White)
    ) {
        Column(
            modifier = Modifier.padding(16.dp)
        ) {
            // 订单头部
            Row(
                modifier = Modifier.fillMaxWidth(),
                horizontalArrangement = Arrangement.SpaceBetween,
                verticalAlignment = Alignment.CenterVertically
            ) {
                Row(verticalAlignment = Alignment.CenterVertically) {
                    Text(
                        modifier = Modifier.background(
                            if (viewState.isLoading) Color.LightGray else Color.Transparent,
                            RoundedCornerShape(2.dp)
                        ),
                        text = order.type,
                        style = MaterialTheme.typography.titleMedium
                    )
                    if (order.status.isNotEmpty()) {
                        Text(
                            text = order.status,
                            modifier = Modifier
                                .padding(start = 8.dp)
                                .background(
                                    color = if (order.status == "已完成" || viewState.isLoading) Color.Green.copy(alpha = 0.1f)
                                    else Color.Red
                                        .copy(
                                            alpha = 0.1f
                                        ),
                                    shape = RoundedCornerShape(4.dp)
                                )
                                .padding(horizontal = 8.dp, vertical = 4.dp),
                            style = MaterialTheme.typography.labelSmall,
                            color = if (order.status == "已完成") Color.Green else Color.Red
                        )
                    }
                }
                Text(
                    modifier = Modifier.background(
                        if (viewState.isLoading) Color.LightGray else Color.Transparent,
                        RoundedCornerShape(2.dp)
                    ),
                    text = order.time,
                    style = MaterialTheme.typography.labelSmall,
                    color = MaterialTheme.colorScheme.onSurfaceVariant
                )
            }

            // 地址路径
            Column(
                modifier = Modifier.padding(top = 16.dp),
                verticalArrangement = Arrangement.spacedBy(8.dp)
            ) {
                Row(verticalAlignment = Alignment.CenterVertically) {
                    Icon(
                        imageVector = if (viewState.isLoading) Icons.Default.Place else Icons.Default.LocationOn,
                        contentDescription = "Start",
                        tint = if (viewState.isLoading) Color.LightGray else Color.Green,
                        modifier = Modifier.size(12.dp)
                    )
                    Text(
                        text = order.startAddress,
                        style = MaterialTheme.typography.bodyMedium,
                        modifier = Modifier
                            .padding(start = 8.dp)
                            .background(
                                if (viewState.isLoading) Color.LightGray else Color.Transparent,
                                RoundedCornerShape(2.dp)
                            )
                    )
                }
                Row(verticalAlignment = Alignment.CenterVertically) {
                    Icon(
                        imageVector = if (viewState.isLoading) Icons.Default.Place else Icons.Default.LocationOn,
                        contentDescription = "End",
                        tint = Color.Black.copy(alpha = 0.5f),
                        modifier = Modifier.size(12.dp)
                    )
                    Text(
                        text = order.endAddress,
                        style = MaterialTheme.typography.bodyMedium,
                        modifier = Modifier
                            .padding(start = 8.dp)
                            .background(
                                if (viewState.isLoading) Color.LightGray else Color.Transparent,
                                RoundedCornerShape(2.dp)
                            )
                    )
                }
            }
        }
    }
}

@Preview
@Composable
fun ShimmerScreenPreview() {
    ShimmerScreen(navController = rememberNavController())
}
